import { CONTENT_TYPE_MULTIPART_FORM_DATA, request } from "@/utils/request";
import type { ResponseBody } from "../typing";
import type {
  FileItem,
  MergeParams,
  MergeResult,
  QueryChunkParams,
  QueryChunkResult,
} from "./typing";

/**查询文件列表 */
export const queryFileList = (fileHeaderId: string) => {
  return request.get<string, ResponseBody<FileItem[]>>("/file/list", {
    params: { fileHeaderId },
  });
};

/**上传分片 */
export const uploadChunk = (params: FormData) => {
  return request.post<FormData, ResponseBody>("/file/upload/chunk", params, {
    headers: CONTENT_TYPE_MULTIPART_FORM_DATA,
    timeout: 60 * 1000,
  });
};

/**合并上传分片 */
export const mergeUpload = (params: MergeParams) => {
  return request.post<MergeParams, ResponseBody<MergeResult>>(
    "/file/upload/merge",
    params,
    { timeout: 60 * 1000 }
  );
};

/**查询是否上传过分片 */
export const queryChunk = (params: QueryChunkParams) => {
  return request.get<QueryChunkParams, ResponseBody<QueryChunkResult>>(
    "/file/chunk",
    { params }
  );
};

/**删除文件 */
export const del = (fileId: string) => {
  return request.delete<string, ResponseBody>("/file/delete", {
    params: { fileId },
  });
};

/**下载文件 */
export const downloadFile = (fileId: string) => {
  return request.get<string, Blob | ResponseBody>("/file/download", {
    params: { fileId },
    responseType: "blob",
  });
};
